package com.wang.template.json.core;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.wang.template.json.express.BaseExpress;
import com.wang.template.json.express.ExpressType;
import com.wang.template.json.express.ListExpress;
import com.wang.template.json.express.MapExpress;
import com.wang.template.json.express.StaticExpress;
import com.wang.template.json.node.DataSource;

public class TemplateUtils {
	private static final Pattern PATTERN_EXPRESS = Pattern.compile("\\$\\{(.*?)\\}");
	private static final Pattern PATTERN_MAP = Pattern.compile("\\[\\'(.*?)\\'\\]");
	private static final Pattern PATTERN_LIST = Pattern.compile("\\[(.*?)\\]");

	public static boolean isJsonArray(String jsonStr) {
		return jsonStr.startsWith(Keys.JSON_FLAG_LIST);
	}

	public static boolean isJsonObject(String jsonStr) {
		return jsonStr.startsWith(Keys.JSON_FLAG_OBJECT);
	}

	/**
	 * 获取表达式中${}中对应的字符串
	 */
	public static String gerKey(String str) {
		Matcher match = PATTERN_EXPRESS.matcher(str);
		if (match.find()) {
			return match.group(1);
		} else {
			throw new RuntimeException(String.format("表达式格式错误->%s", str));
		}
	}

	/**
	 * 表达式是否是map
	 * 
	 * @param expressStr
	 * @return
	 */
	public static DataSource buildDataSource(String expressStr) {
		if (StringUtils.isBlank(expressStr)) {
			throw new RuntimeException("表达式为空，不能解析");
		}
		DataSource ds = new DataSource();
		BaseExpress[] express = null;
		// 1-去除${}项
		Matcher match = PATTERN_EXPRESS.matcher(expressStr);
		if (match.find()) {
			expressStr = match.group(1);
			boolean isDeep = expressStr.indexOf(Keys.JSON_KEY_SPLIT) > -1;
			ds.setDeep(isDeep);
			ds.setExpressStr(expressStr);
			String[] keys = expressStr.split(Keys.JSON_KEY_SPLIT_REG);
			express = new BaseExpress[keys.length];
			for (int index = 0; index < keys.length; index++) {
				BaseExpress e = buildExpress(keys[index]);
				express[index] = e;
			}
		} else {
			// 5-是个静态值
			StaticExpress staticV = new StaticExpress();
			staticV.setExpressType(ExpressType.STATIC);
			staticV.setStaticValue(expressStr);
			express = new BaseExpress[1];
			express[0] = staticV;
		}
		ds.setExpress(express);
		ds.setExpressStr(expressStr);
		return ds;
	}

	/**
	 * 将表达式字符串转换为表达式对象
	 * 
	 * @param expressStr
	 * @return
	 */
	public static BaseExpress buildExpress(String expressStr) {
		// 1-表达式是个map类型
		Matcher match = PATTERN_MAP.matcher(expressStr);
		if (match.find()) {
			MapExpress map = new MapExpress();
			map.setExpressType(ExpressType.MAP);
			map.setDs(expressStr.substring(0, expressStr.lastIndexOf(Keys.JSON_FLAG_MAP)));
			map.setDsMethodName(getMethodName(map.getDs()));
			String key = match.group(1);
			map.setKey(key);
			return map;
		}
		// 2-表达式是list类型
		match = PATTERN_LIST.matcher(expressStr);
		if (match.find()) {
			ListExpress list = new ListExpress();
			list.setExpressType(ExpressType.LIST);
			list.setDs(expressStr.substring(0, expressStr.lastIndexOf(Keys.JSON_FLAG_LIST)));
			list.setDsMethodName(getMethodName(list.getDs()));
			String key = match.group(1);
			int index = Integer.parseInt(key);
			list.setIndex(index);
			return list;
		}
		// 3-是个普通的变量表达式
		BaseExpress normal = new BaseExpress();
		normal.setExpressType(ExpressType.STRING);
		normal.setDs(expressStr);
		normal.setDsMethodName(getMethodName(expressStr));
		
		return normal;
	}

	public static String getMethodName(String propertyName) {
		return "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
	}
}
